# coding=utf8

import tools
import BaseHTTPServer
import urlparse
import ftplib
import paramiko

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        #print "Get path is:%s"%self.path
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        url = urlparse.urlparse(self.path)

        self.wfile.write("欢迎你！")
        self.wfile.write("\npath : " + url.path)
        qs = urlparse.parse_qs(url.query)
        for k in qs.keys():
            self.wfile.write("\n{0}={1} : ".format(k, qs[k]))
        if "quit" in qs.keys():
            self.server.quit = True


if __name__ == "__main__":
    # a = tools.SystemInfo()
    # a.ShowInfo()
    # tools.CreateFile("1.txt", "中文内容")
    # print tools.ReadFile("1.txt")

    # 邮件发送
    # mailSender=tools.MailSender("10.141.72.4", 25,"zhangbin@chinatelecom.cn", "1728essl")
    # if mailSender.sendMail("bb_boy@163.com","邮件标题","邮件正文。。。"):
    #     print("邮件发送成功！")
    # else:
    #     print("错误：邮件发送失败:"+mailSender.lastError)

    # Http client 请求
    # client=tools.HTTPClient("133.64.81.237",8080,5)  #代理方式
    # client.exceptionAddress.append("mss.sntele.com")  #例外地址
    # # client=tools.HTTPClient(timeout=5)  #无代理方式
    # if client:
    #     ret=client.get("http://www.baidu.com")
    #     if ret:
    #         print client.status,client.reason
    #         print ret
    #     else:
    #         print "错误："+client.lastError
    # else:
    #     print "创建连接失败!"

    # # Http Serve
    # server = BaseHTTPServer.HTTPServer(("127.0.0.1", 8888), MyHandler)
    # server.quit = False
    # print "server start at {0}".format(server.server_address)
    # while not server.quit:
    #     server.handle_request()
    # print "server close !"

    # # FTP
    # ftp=ftplib.FTP()
    # ftp.set_debuglevel(2)
    # ftp.connect("192.168.128.101",21)
    # ftp.login("zhangbin","17282430")  # 匿名登录时密码用空串""
    # ftp.cwd("ftp")  # 切换目录栏
    # ftp.set_pasv(False)  # 关闭主动模式（必须）
    # ftp.retrbinary("RETR ftp.bin",open("ftp.bin","wb").write)  #下载
    # ftp.storbinary("STOR ftp2.bin",open("ftp.bin","rb"))   # 上传
    # ftp.quit()

    # SSH
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect("192.168.128.101",22,"zhangbin","17282430")
    stdin, stdout, stderr = ssh.exec_command("ls ftp -l") # 执行命令
    result = stdout.read()                         # 从stdout获取结果
    if not result:
        result = stderr.read()                     # 如果stdout没有结果，从stderr获取错误
    print result
    ssh.close()
    #SFTP
    transport=paramiko.Transport(("192.168.128.101",22))
    transport.connect(username="zhangbin", password="17282430")
    sftp=paramiko.SFTPClient.from_transport(transport)
    sftp.get("ftp/ftp.bin","ftp.bin")
    sftp.put("ftp.bin","ftp/ftp2.bin")
    transport.close()
    